﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using ApplicationLibrary;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;
using Vintner.Library;
using Vintner.Masters.ModuleData;

namespace Vintner.Masters.Forms
{
    public partial class Customers : DocumentForm
    {
        public ApplicationLibrary.ExtendedControls.TabControl MasterTabControl { get { return tabControl1; } }

        public Customers()
            : base("Customers")
        {
            InitializeComponent();
            keyControl = fncCustomer;
        }

        #region Virtual Method

        protected override IRadarParameters GetKeyFromDocument()
        {
            return new RadarCustomerParam(fncCustomer.Text);
        }

        protected override void OnAttachData()
        {
            dbManager = new dbmanagerCustomers("customerForm", new RadarCustomerForm(), this);
            dbManager.AddMaster(this, VI_Customers.Name, true);
            fncCustomer.AttachCounterType(GlobalInfo.GetCounter(Properties.Resources.CN_Customers), GlobalInfo.CurrentDate);

            rdrTitle.RadarForm = new RadarDescriptions(DescriptionType.E_Title);
            rdrTitle.DescriptionControl = lblTitleDesc;
        }

        protected override void OnBindData()
        {
            BindControl(fncCustomer, VI_Customers.Customer, Findable.YES);
            BindControl(txtCompany, VI_Customers.Description, Findable.YES);
            BindControl(ckbBlocked, VI_Customers.Blocked, "DBChecked");
            BindControl(ckbDisabled, VI_Customers.Disabled, "DBChecked");

            BindControl(rdrTitle, VI_Customers.Title);
            BindControl(txtAddress, VI_Customers.Address);
            BindControl(mtbZip, VI_Customers.ZipCode);
            BindControl(txtCity, VI_Customers.City);
            BindControl(txtCounty, VI_Customers.County);
            BindControl(txtPhone1, VI_Customers.Phone1);
            BindControl(txtPhone2, VI_Customers.Phone2);
            BindControl(txtFax, VI_Customers.Fax);
            BindControl(txtRefer1, VI_Customers.Refer1);
            BindControl(txtRefer2, VI_Customers.Refer2);

            BindControl(txtDestGoods1, VI_Customers.DestGoods1);
            BindControl(txtDestGoods2, VI_Customers.DestGoods2);

            BindControl(mtbTaxNo, VI_Customers.TaxNo);
            BindControl(mtbFiscalCode, VI_Customers.FiscalCode);

            BindControl(txtEmail, VI_Customers.Email);

            BindCounter(ref fncCustomer);
        }

        protected override void OnDisableControlsForEdit()
        {
            fncCustomer.Enabled = false;
            base.OnDisableControlsForEdit();
        }

        protected override void OnDisableControlsForNew()
        {
            base.OnDisableControlsForNew();
        }

        protected override void FocusOnNew()
        {
            fncCustomer.Focus();
        }

        protected override bool OnBeforeUndo()
        {
            return base.OnBeforeUndo();
        }

        protected override bool OnBeforeSave()
        {
            if (!FiscalCodeChecker.CheckFiscalCode(mtbFiscalCode.Text))
            {
                MyMessageBox(this, Properties.Resources.Err_FiscalCode,
                Properties.Resources.Error,
                MessageBoxButtons.OK, MessageBoxIcon.Error);

                //return false;
            }
            if (!TaxNoChecker.CheckTaxNo(mtbTaxNo.Text))
            {
                MyMessageBox(this, Properties.Resources.Err_TaxNo,
                Properties.Resources.Error,
                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return base.OnBeforeSave();
        }

        protected override void OnPrepareAuxData()
        {
            base.OnPrepareAuxData();
        }

        #endregion

        #region Events

        private void mtbFiscalCode_Validating(object sender, CancelEventArgs e)
        {
            if (!FiscalCodeChecker.CheckFiscalCode(mtbFiscalCode.Text))
            {
                MyMessageBox(this, global::Vintner.Properties.Resources.Err_FiscalCode,
                global::Vintner.Properties.Resources.Warning,
                MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void mtbTaxNo_Validating(object sender, CancelEventArgs e)
        {
            if (!TaxNoChecker.CheckTaxNo(mtbTaxNo.Text))
            {
                MyMessageBox(this, global::Vintner.Properties.Resources.Err_TaxNo,
                global::Vintner.Properties.Resources.Warning,
                MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        #endregion

        private void Customers_Load(object sender, EventArgs e)
        {
        }
    }

    #region DbManager Customers

    /// <summary>
    /// Classe derivata per gestioni particolari
    /// </summary>
    internal class dbmanagerCustomers : DBManager
    {
        public dbmanagerCustomers(string name, RadarForm radar, DocumentForm document)
            : base(name, radar, document)
        {
        }

        protected override string CreateMasterQuery()
        {
            this.Table = new VI_Customers();
            QueryBuilder qB = new QueryBuilder();
            qB.AddSelect();
            qB.AddFrom(VI_Customers.Name);
            qB.AddCompare(VI_Customers.Customer, "@p1");

            return qB.Query;
        }

        protected override List<SqlABParameter> CreateMasterParam()
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();

            SqlABParameter sParam = new SqlABParameter("@p1", VI_Customers.Customer);
            sParam.Value = "";
            PList.Add(sParam);
            return PList;
        }

        protected override void SetParameters(IRadarParameters key, DBCollection collection)
        {
            if (collection.Name == VI_Customers.Name)
                collection.Parameter[0].Value = key[0];
        }
    }

    #endregion
}